define(function (require, exports, module) {
    var myPub = require('./public');

    var oWindow = $(window);                        // window
    var oWrap = $('.js_wrap');                      // wrap 部分
    var oContain = $('.js_contain');                // 大盒子
    var oHeader = $('.js_headerBox');               // 头部盒子
    var oFooter = $('.js_footer');                  // 页面底部
    var oErrPage = $('.js_errPage');                // 错误页面


    var oCollect_goods = $('.js_collect_goods');                        // 收藏产品
    var oCollect_goodsList = oCollect_goods.find('.js_goodsList');      // 收藏产品列表

    var oLike_goods = $('.js_like_goods');                              // 猜你喜欢产品
    var oLike_goodsList = oLike_goods.find('.js_goodsList');            // 猜你喜欢产品列表

    var oHistory_goods = $('.js_history_goods');
    var oHistory_goodsList = oHistory_goods.find('.js_goodsList');
    var oBtn_reHome = $('.js_btn_reHome');



    var oGoTop = $('.js_goTop');                    // 回到顶部
    var oSideBox = $('.js_sideBox');		        // 侧边按钮框

    var bChangeOff = false;		                    // 是否改变加载数据

    var lazLoading = new myPub.lazLoadingImg();

    var resizeTimer = null;

    var oJumpA = $('.jumpHref a');



    // 获取产品数据的路径
    //var collectDateUrl = '/shop/goodsCollection/collectionList';
    //var collectGuessLikeUrl = '/shop/goodsCollection/guessLikeGoodsList';
    var collectDateUrl = '/m/g/goods/collection/1.0/collectList';
    var collectGuessLikeUrl = '/m/g/goods/collection/1.0/collectRecommendList';
    var historyDataUrl = '/myInfo/browseHistoryList';
    var historyGuessLikeUrl = '/myInfo/guessLikeGoodsList';

    module.exports = {
        init: function (data, name) {
            myPub.JudgeAppHref(oJumpA);
            //初始化事件
            myCollection.InitPage();

            lazLoading.AddLazLoadingImg(oWrap[0]);


            // 页面尺寸修改
            oWindow.on('resize', function () {
                if (resizeTimer) {
                    clearTimeout(resizeTimer)
                }
                resizeTimer = setTimeout(function () {
                    myCollection.InitPage(true);
                }, 400);

            });

            //滚动加载数据
            myCollection.scrollLoadData();

            //页面进来动态获取数据
            // coAjax.getMyLike();
            if(oHistory_goods.length > 0) {
                coAjax.getMyLike(function(){
                    coAjax.getMyHistory(function () {
                        if (oContain.height() < oWindow.height()) {
                            coAjax.loadNew();
                        }
                    });
                },historyGuessLikeUrl);
            }
            else {
                coAjax.getMyLike(function(){
                    coAjax.getMyCollection(function () {
                        if (oContain.height() < oWindow.height()) {
                            coAjax.loadNew();
                        }
                    });
                },collectGuessLikeUrl);
            }

        }
    }

    var coAjax = {
        allData_collect: null,                      // 请求收藏数据
        allData_history: null,                      // 请求历史足迹数据
        allData_like: null,                         // 请求猜你喜欢的数据
        totalPageNum: 0,                    // 总数据的页数
        startNum: 0,                        // 数据加载的起始位置
        limitNum: 4,                        // 一次加载多少条数据
        endNum: 0,                          // 数据加载的结束位置
        coPageArr: 1,                       // 用于限定请求几次数据的限制
        coPageScroll: 0,                    // 保存滚动高度
        // 我的收藏
        getMyCollection: function (fn) {    // 数据请求的事件
            var _this = this;
            G_JSON_GET(collectDateUrl, {}, function (data) {
                //_this.allData_collect = data.goodsNew2;

                //if (data.status == "success") {
                _this.allData_collect = data.data;
                if(data != null && data.ret.code == "0"){
                    if(_this.allData_collect.length < 1) {
                        oWrap.hide();
                        oErrPage.show();
                        return
                    }
                    // 第一次进来的数据加载的最后的位置
                    _this.endNum = _this.limitNum;
                    _this.totalPageNum = Math.ceil(_this.allData_collect.length / _this.limitNum);
                    if (_this.limitNum > _this.allData_collect.length){
                        _this.endNum = _this.allData_collect.length;
                    }
                    connectHtml.GoodsList(oCollect_goodsList, _this.allData_collect, _this.startNum, _this.endNum);
                    if (fn) fn();
                    data = null
                }

            }, function () {}, function () {})
        },

        // 历史数据
        getMyHistory: function (fn) {    // 数据请求的事件
            var _this = this;
            G_JSON_GET(historyDataUrl, {}, function (data) {
                _this.allData_history = data.goodsNew2;

                if (data.status == "success") {
                    if(_this.allData_history.length < 1) {
                        oWrap.hide();
                        oErrPage.show();
                        return
                    }
                    // 第一次进来的数据加载的最后的位置
                    _this.endNum = _this.limitNum;
                    _this.totalPageNum = Math.ceil(_this.allData_history.length / _this.limitNum);
                    if (_this.limitNum > _this.allData_history.length){
                        _this.endNum = _this.allData_history.length;
                    }
                    connectHtml.GoodsList(oHistory_goodsList, _this.allData_history, _this.startNum, _this.endNum);
                    if (fn) fn();
                    data = null
                }

            }, function () {}, function () {})
        },

        // 猜你喜欢
        getMyLike: function (fn,url) {    // 数据请求的事件
            var _this = this;
            G_JSON_GET(url, {}, function (data) {
                //_this.allData_like = data.goodsNew2;
                //if (data.status == "success") {
                _this.allData_like = data.data;
                if(data != null && data.ret.code == "0"){
                    if(_this.allData_like.length < 1) {
                        oLike_goods.hide();
                        return
                    }

                    data = null
                }

            }, function () {}, function () {
                if (fn) fn();
            })
        },

        // 加载数据控制
        loadNew: function () {
            var _this = this;
            var AppendObj = null;
            var getData = null;

            //判断第一次是否翻到最后
            if (_this.coPageArr >= _this.totalPageNum) {

                // coAjax.allData_collect = null;
                if(bChangeOff) return;
                bChangeOff = true;
                // 初始化数据
                _this.startNum = 0;
                _this.coPageArr = 0;
                _this.endNum = 0;
                _this.totalPageNum = Math.ceil(_this.allData_like.length / _this.limitNum);
                oLike_goods.show();
                // console.log('完毕了-收藏');
            }

            if (bChangeOff) {
                AppendObj = oLike_goodsList;
                getData = _this.allData_like;
            }
            else {

                if(oHistory_goods.length > 0) {
                    // 历史足迹数据
                    AppendObj = oHistory_goodsList;
                    getData = _this.allData_history;
                }
                else {
                    // 收藏数据
                    AppendObj = oCollect_goodsList;
                    getData = _this.allData_collect;
                }

            }
            //每加载一次,所翻的页面+1
            _this.coPageArr++;
            //加载了一次就更新数据加载开始的位置
            _this.startNum = _this.endNum;
            //加载了次后更新数据加载结束的位置
            _this.endNum = _this.limitNum * _this.coPageArr;

            if(_this.endNum >= getData.length) {
                _this.endNum = getData.length;
                if (bChangeOff){
                    oBtn_reHome.show();
                }
            }

            //滚动到那高度再拼接数据
            connectHtml.GoodsList(AppendObj, getData, _this.startNum, _this.endNum, function () {
                if (oContain.height() < oWindow.height()) {
                    _this.loadNew();
                }
            });
        }

    }

    //对象事件
    var myCollection = {
        InitPage: function (bOff) {  //初始化事件
            if (bOff) {
                oContain.css({
                    'padding-top': oHeader.outerHeight(true),
                    'padding-bottom': oFooter.outerHeight(true)
                });
            }
            oSideBox.stop().animate({'right': '-3.5rem'}, 300);
        },
        scrollLoadData: function () {  //滚动加载数据
            var $this = this;
            var minHeight = 300;
            var AppendObj = null;
            var getData = null;
            oWrap.on('scroll', function (event) {
                var nowScrollTop = $(this).scrollTop();
                clearTimeout($this.time_Scroll);
                $this.time_Scroll = setTimeout(function () {
                    // coAjax.coPageScroll = nowScrollTop;
                    if (oWrap[0].scrollHeight - nowScrollTop - oWrap.height() < minHeight) {
                        coAjax.loadNew();
                    }
                }, 150)
            })
        }
    }


    //拼接的html
    var connectHtml = {
        GoodsList: function (obj, data, startNum, endNum, fn) {
            var _liHtml = '';
            var addCarHtml = '<p class="addCar js_addCar"><em class="icon-shopcar_small"></em></p>';
            for(var i=startNum; i<endNum; i++) {
                var title = data[i].title;
                if (title == null){
                    title = data[i].productName;
                }
                /*var addCarHtml = '';*/
                var priceDownHtml = '';
                /*if(!obj.parents('.js_myGoods_show').hasClass('act_three')) {
                    addCarHtml = '<p class="addCar js_addCar" pid="'+data[i].productId+'"><em class="icon-shopcar_small"></em></p>';
                }*/
                if (data[i].collePrice != null && data[i].collePrice > 0){
                    priceDownHtml = '<p class="priceDownTip">比收藏时便宜了'+data[i].collePrice+'元</p>';
                }
                var goodsImage;
                if(oHistory_goods.length > 0){
                    goodsImage = data[i].showImage;
                }else{
                    goodsImage = data[i].goodsImagePath;
                }
                if(goodsImage.indexOf(",") != -1){
                    goodsImage = goodsImage.split(",")[0];
                }
                _liHtml +=
                    '<li>'+
                    '    <a class="BoxSelf" href="'+BASE_URL+'/h/goodspay/goodsView/'+data[i].goodsSn+'">'+
                    '        <figure>'+
                    //'            <img class="js_lazyImg" data-echo="'+ HTTP_IMAGE_URL+data[i].showImage + smallImgArg +'" src="'+BASE_URL+'/static_v4/image/loadImg.jpg">'+
                    '            <img class="js_lazyImg" data-echo="'+ HTTP_IMAGE_URL + goodsImage + smallImgArg +'" src="'+BASE_URL+'/static_v4/image/loadImg.jpg">'+
                                priceDownHtml;
                    if(data[i].firstGueeImagePath!=null && data[i].firstGueeImagePath!=''){
                        _liHtml += '<img src="'+data[i].firstGueeImagePath+'"alt="" class="quanpei"/>';
                    }
                _liHtml += '        </figure>'+
                    '        <aside class="priceArea VBox Bflex1">'+
                    '            <div class="Bflex1 priceArea_name">'+
                    '                <h4>'+ data[i].name +'</h4>'+
                    '                <p class="smallFont_tip">'+title+'</p>'+
                    '            </div>'+
                    '            <div class="priceArea_price">'+
                   // '                <i class="tip-info">限时立减150</i>'+
                    '                <p class="HBox priceAreaBox">'+
                    '                    <span class="goodsPrice">￥'+data[i].salePrice+'</span>'+
                    '                </p>'+
                    '            </div>'+
                    '        </aside>'+
                    '    </a>'+
                        // addCarHtml+
                    '</li>'
            }
            var oShtml = $(_liHtml);
            obj.append(oShtml);

            myPub.SetImgHeight(oShtml);

            lazLoading.DestroyLazLoadingImg();
            lazLoading.AddLazLoadingImg(oWrap[0]);
            // lazLoading.RushLazLoadingImg();

            if(fn)fn();
        }
    }
})